<?php
/**
 * lemocms
 * ============================================================================
 * 版权所有 2018-2027 lemocms，并保留所有权利。
 * 网站地址: https://www.lemocms.com
 * ----------------------------------------------------------------------------
 * 采用最新Thinkphp6实现
 * ============================================================================
 * Author: yuege
 * Date: 2019/8/2
 */
namespace app\admin\controller;
use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use app\BaseController;
use lemo\helper\SignHelper;
use think\facade\Session;
use think\facade\View;
use think\facade\Request;
use think\captcha\facade\Captcha;

class Login extends BaseController {
    /*
     * 登录
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function index(){
        if (!Request::isPost()) {
            $admin= Session::get('admin');
            $admin_sign= Session::get('admin_sign') == SignHelper::authSign($admin) ? $admin['id'] : 0;
            // 签名验证
            if ($admin && $admin_sign) {
                return redirect('index/index');
            }

            return View::fetch();

        } else {

            $username = Request::post('username', '', 'lemo\helper\StringHelper::filterWords');
            $password = Request::post('password', '', 'lemo\helper\StringHelper::filterWords');
            $captcha = Request::post('captcha', '', 'lemo\helper\StringHelper::filterWords');
            $rememberMe = Request::post('rememberMe');
            // 用户信息验证

            try {
                if(!captcha_check($captcha)){
                    throw new \Exception(lang('captcha error'));
                }
                $res = self::checkLogin($username, $password,$rememberMe);
            } catch (\Exception $e) {
                $this->error(lang('login fail')."：{$e->getMessage()}");
            }
            $this->success(lang('login success').'...', url('@admin'));
        }
    }

    /*
     * 验证码
     *
     */
    public function verify()
    {

        return Captcha::create();
    }



    /**
     * 根据用户名密码，验证用户是否能成功登陆
     * @param string $user
     * @param string $pwd
     * @throws \Exception
     * @return mixed
     */
    public static function checkLogin($user, $password,$rememberMe) {

        try{
            $where['username'] = strip_tags(trim($user));
            $password = strip_tags(trim($password));
            $info = Admin::where($where)->find();
           
            if(!$info){
                throw new \Exception(lang('please check username or password'));
            }
            if($info['status']==0){
                throw new \Exception(lang('account is disabled'));
            }
            if(!password_verify($password,$info['password'])){

                throw new \Exception(lang('please check username or password'));

            }
            if(!$info['group_id']){
                $info['group_id'] = 1;

            }
            $rules = AuthGroup::where('id',$info['group_id'])
                ->value('rules');
            $info['rules'] = $rules  ;
            if(!$info['username']){
                $info['username'] = $info['username'];
            }
            if($rememberMe){
                Session::set('admin', $info,7*24*3600);
                Session::set('admin_sign',  SignHelper::authSign($info),5*24*3600);
            }else{
                Session::set('admin', $info);
                Session::set('admin_sign',  SignHelper::authSign($info));
            }

        }catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }

        return true;
    }
}